package com.us.usws.site.console.action;

import javax.inject.Inject;

import net.iwsh.core.security.digest.DigestUtils;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.system.User;
import com.us.usws.service.cud.UserCUDService;
import com.us.usws.service.ro.DeptROService;
import com.us.usws.service.ro.UserROService;
import com.us.usws.support.uitls.JSONDataUtil;

/**
 * manage system's users
 * @author fengyong
 *
 */
public class SysUserManagerAction extends BasePageSearchAction<User> {

	private static final long serialVersionUID = 8387241110291343357L;

	private User user;

	@Inject
	private UserROService userROService;

	@Inject
	private UserCUDService userCUDService;

	@Inject
	private DeptROService deptROService;

	private String uids;

	/**
	 * ajax get users
	 */
	public void ajaxLoadUser() {
		createDataPageUtil();
		userROService.loadUsersByPaging(dataPageUtil);
		getOut().println(JSONDataUtil.extJsonSupportPage(dataPageUtil.getResult(), dataPageUtil.getRowCount()));
	}

	/**
	 * ajax get depts
	 */
	public void ajaxLoadDept() {
		getOut().println(JSONDataUtil.jsonStringfromObject(deptROService.getAllDepts()));
	}

	/**
	 * ajax update user info
	 */
	public void ajaxUpdateUser() {
		if (user != null) {
			String uid = user.getUid();
			/*
			 * when uid is not null and not empty, then update nor add
			 */
			if (StringUtils.isNotBlank(uid)) {
				userCUDService.update(user);
			} else {
				user.setIsSiteAdmin(SiteConstants.STR_BOOLEAN_YES);
				user.setIsValid(SiteConstants.STR_BOOLEAN_YES);
				user.setPasswd(DigestUtils.md5Salt(user.getPasswd()));
				userCUDService.add(user);
			}
			getOut().print(true);
		} else {
			getOut().print(false);
		}
	}

	/**
	 * ajax delete user info
	 */
	public void ajaxDeleteUser() {
		userCUDService.deleteMore(uids);
	}
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUids() {
		return uids;
	}

	public void setUids(String uids) {
		this.uids = uids;
	}
}
